package com.foreknow.elm.controller;

import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserSevice;
import com.foreknow.elm.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;

public class UserController {
    private int a;

    /**
     * 根据用户编号与密码查询用户信息
     */
    public Object getUserByIdByPass(HttpServletRequest request) throws Exception{
        // 获取请求参数 UserController/getUserByIdByPass?userId=12345671111&password=123
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        UserSevice sevice = new UserServiceImpl();
        // 调用service方法
        User user = sevice.getUserByIdByPass(userId,password);
        return user;
    }

    /**
     * 根据用户编号查询用户表返回的行数
     */
    public Object getUserById(HttpServletRequest request) throws Exception{
        // 获取请求参数 UserController/getUserById?userId=12345671111
        String userId = request.getParameter("userId");
        UserSevice sevice = new UserServiceImpl();
        // 调用service方法
        a = sevice.getUserById(userId);
        // 返回查询到的行数
        return a;
    }

    /**
     * 向用户表中添加一条记录
     */
    public Object saveUser(HttpServletRequest request) throws Exception{
        // 获取请求参数 UserController/saveUser?userId=12345673333&password=12345&userName=小王&userSex=1&userImg=1122334455&delTag=1
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        String userName = request.getParameter("userName");
        Integer userSex = Integer.parseInt(request.getParameter("userSex"));
        User user = new User();
        user.setUserId(userId);
        user.setPassword(password);
        user.setUserName(userName);
        user.setUserSex(userSex);
        UserSevice sevice = new UserServiceImpl();
        // 调用service方法
        a = sevice.saveUser(user);
        return a;
    }

}
